<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Get an ASCII-encoded hash</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sodium-crypto-pwhash-str-verify.html">« sodium_crypto_pwhash_str_verify</a></li>
      <li style="float: right;"><a href="function.sodium-crypto-pwhash.html">sodium_crypto_pwhash »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sodium.html">Sodium 函数</a></li>
    <li>Get an ASCII-encoded hash</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sodium-crypto-pwhash-str" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_pwhash_str</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_pwhash_str</span> &mdash; <span class="dc-title">Get an ASCII-encoded hash</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-pwhash-str-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_pwhash_str</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$opslimit</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$memlimit</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
Uses a CPU- and memory-hard hash algorithm along with a randomly-generated salt, and memory and CPU limits to generate an ASCII-encoded hash suitable for password storage.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-pwhash-str-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">password</code></dt>

    <dd>

     <p class="para">
      <span class="type">string</span>; The password to generate a hash for.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">opslimit</code></dt>

    <dd>

     <p class="para">
      Represents a maximum amount of computations to perform. Raising this number will make the function require more CPU cycles to compute a key. There are constants available to set the operations limit to appropriate values depending on intended use, in order of strength: <strong><code>SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE</code></strong>, <strong><code>SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE</code></strong> and <strong><code>SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE</code></strong>.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">memlimit</code></dt>

    <dd>

     <p class="para">
      The maximum amount of RAM that the function will use, in bytes. There are constants to help you choose an appropriate value, in order of size: <strong><code>SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE</code></strong>, <strong><code>SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE</code></strong>, and <strong><code>SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE</code></strong>. Typically these should be paired with the matching opslimit values.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-pwhash-str-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the hashed password.
  </p>
  <p class="para">
   In order to produce the same password hash from the same password, the same values for <code class="parameter">opslimit</code> and <code class="parameter">memlimit</code> must be used. These are embedded within the generated hash, so
   everything that&#039;s needed to verify the hash is included. This allows
   the <span class="function"><a href="function.sodium-crypto-pwhash-str-verify.html" class="function">sodium_crypto_pwhash_str_verify()</a></span> function to verify the hash without
   needing separate storage for the other parameters.
  </p>
 </div>


  <div class="refsect1 examples" id="refsect1-function.sodium-crypto-pwhash-str-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-990">
    <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_pwhash_str()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sodium_crypto_pwhash_str</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE<br /></span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
$argon2id$v=19$m=65536,t=2,p=1$oWIfdaXwWwhVmovOBc2NAQ$EbsZ+JnZyyavkafS0hoc4HdaOB0ILWZESAZ7kVGa+Iw
</pre></div>
    </div>
   </div>
  </p> 
 </div>


 <div class="refsect1 notes" id="refsect1-function.sodium-crypto-pwhash-str-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Hashes are calculated using the Argon2ID algorithm,
    providing resistance to both GPU and side-channel attacks.
    In contrast to the <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> function,
    there is no salt parameter (a salt is generated automatically),
    and the <code class="parameter">opslimit</code> and
    <code class="parameter">memlimit</code> parameters are not optional.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.sodium-crypto-pwhash-str-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.sodium-crypto-pwhash-str-verify.html" class="function" rel="rdfs-seeAlso">sodium_crypto_pwhash_str_verify()</a> - Verifies that a password matches a hash</span></li>
    <li class="member"><span class="function"><a href="function.sodium-crypto-pwhash.html" class="function" rel="rdfs-seeAlso">sodium_crypto_pwhash()</a> - Derive a key from a password, using Argon2</span></li>
    <li class="member"><span class="function"><a href="function.password-hash.html" class="function" rel="rdfs-seeAlso">password_hash()</a> - 创建密码的散列（hash）</span></li>
    <li class="member"><span class="function"><a href="function.password-verify.html" class="function" rel="rdfs-seeAlso">password_verify()</a> - 验证密码是否和散列值匹配</span></li>
    <li class="member"><a href="https://download.libsodium.org/doc/password_hashing/the_argon2i_function.html" class="link external">&raquo;&nbsp;Libsodium Argon2 docs</a></li>
   </ul>
  </p>
 </div>



</div></div></div></body></html>